package com.view.sellmanage;

import com.bean.Purchase;
import com.bean.Sell;
import com.bean.SellInfo;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.service.PurchaseService;
import com.service.SellService;
import com.service.impl.PurchaseServiceImpl;
import com.service.impl.SellServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class SellQueryManagePanel extends JPanel {
    private LovoTable purQueryTab;

    private LovoButton showBtn;


    private JPanel queryPanel;
    private JLabel nameLab;
    private JLabel beginLab;
    private JLabel endLab;
    private JTextField nameTxt;
    private JTextField beginTxt;
    private JTextField endTxt;
    private JButton queryBtn;

    private MainFrame frame;

    private LovoButton prev,next;

    private JLabel txtPage;
    private PageUtilSuper<Sell> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;

    private SellService sellService = new SellServiceImpl();

    public SellQueryManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        setPageUtilSuper();
        this.initTable();
        this.initBtn();
        this.initQueryPanel();
        //this.bindTable();
        txtPage = new JLabel();
        this.txtPage.setBounds(170, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private void initTable() {
        // TODO Auto-generated method stub
        this.purQueryTab = new LovoTable(this, new String[] { "客户名称", "销售员", "出库时间","订单总额"
        }, new String[] {"customer.name","user.realName","sellDate","money"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        List<Sell> sells = new ArrayList<>();
        sells = pageUtilSuper.getPgNumberThing(1);
        purQueryTab.updateLovoTable(sells);
        this.purQueryTab.setSizeAndLocation(20, 0, 750, 300);

        //bindTable();
    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        java.util.List<Sell> list = new ArrayList<>();
        list = sellService.getAllSell();
        System.out.println(list);
        pageUtilSuper = new PageUtilSuper(pageSize,list);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        List<Sell> list = new ArrayList<>();
        list = pageUtilSuper.getPgNumberThing(pgNumber);
        purQueryTab.updateLovoTable(list);
    }

    private void initBtn(){

        this.showBtn = new LovoButton("销售详细信息", 250, 380, this);

        this.prev = new LovoButton("上一页",30,320,this);
        this.next = new LovoButton("下一页",250,320,this);

        showBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = purQueryTab.getKey();
                if (key == -1) {
                    JOptionPane.showMessageDialog(null,"请选择一行");
                }else {
                    SellInfoDialog sellInfoDialog = new SellInfoDialog(frame,key);
                }
            }
        });

        prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.havePrev(pgNumber)) {
                    pgNumber--;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是第一页");
                }
            }
        });

        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.haveNext(pgNumber)) {
                    pgNumber ++;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是最后一页");
                }
            }
        });
    }

    private void initQueryPanel() {
        // TODO Auto-generated method stub
        this.queryPanel = new JPanel();
        this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询销售信息"));
        this.queryPanel.setBackground(Color.WHITE);
        this.queryPanel.setLayout(null);
        this.queryPanel.setBounds(380, 310, 300, 180);
        this.add(this.queryPanel);

        // 标签
        this.nameLab = new JLabel("客户名称");
        this.nameLab.setBounds(30, 30, 50, 30);
        this.queryPanel.add(this.nameLab);
        this.beginLab = new JLabel("起始日期");
        this.beginLab.setBounds(30, 60, 50, 30);
        this.queryPanel.add(this.beginLab);
        this.endLab = new JLabel("结束日期");
        this.endLab.setBounds(30, 90, 50, 30);
        this.queryPanel.add(this.endLab);


        // 文本框
        this.nameTxt = new JTextField();
        this.nameTxt.setBounds(100, 35, 130, 20);
        this.queryPanel.add(this.nameTxt);
        this.beginTxt = new JTextField();
        this.beginTxt.setBounds(100, 65, 130, 20);
        this.queryPanel.add(this.beginTxt);
        this.endTxt = new JTextField();
        this.endTxt.setBounds(100, 95, 130, 20);
        this.queryPanel.add(this.endTxt);


        // 查询按钮
        this.queryBtn = new JButton("查询");
        this.queryBtn.setBounds(200, 140, 80, 20);
        this.queryPanel.add(this.queryBtn);

        String nameTxtText = nameTxt.getText();
        String beginTxtText = beginTxt.getText();
        String endTxtText = endTxt.getText();
        List<Sell> list = new ArrayList<>();
        list = sellService.getFindSell(nameTxtText,beginTxtText,endTxtText);
        purQueryTab.updateLovoTable(list);
    }
}
